package mblog.web.controller.api;

import mblog.base.lang.Consts;
import mblog.core.data.Post;
import mblog.core.persist.service.PostService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/api")
public class ContentsController {

    @Autowired
    private PostService postService;

    @RequestMapping(value="contents")
    public Page<Post> contents(HttpServletRequest request){
        String pnStr = request.getParameter("pn");
        if(StringUtils.isEmpty(pnStr)){
            pnStr = "1";
        }
        Integer pn = Integer.valueOf(pnStr);//" getInteger("pn", 1);


        String channelIdStr = request.getParameter("channelId");
        if(StringUtils.isEmpty(channelIdStr)){
            channelIdStr = "0";
        }
        Integer channelId = Integer.valueOf(channelIdStr);

        String order = request.getParameter("order");

        String pfStr = request.getParameter("pf");
        if(StringUtils.isEmpty(pfStr)){
            pfStr = "1";
        }
        Integer pf = Integer.valueOf(pfStr);//1公开 2不公开


        Pageable pageable = new PageRequest(pn - 1, 16);
        Page<Post> result = postService.paging(pageable, channelId, order,pf);

        return result;//.getContent();
    }

}
